import os
import cv2
import numpy as np
import random
from pathlib import Path
from PIL import Image

# 指定图片文件夹路径
image_folder = 'save'

def cover_walker(walker, image):
    # 获取行人和原图的长宽
    walker_width, walker_height = walker.size
    image_width, image_height = image.size
    # 获取行人图片的比例
    walker_ratio = walker_width/walker_height
    # 获取行人图片缩放后的长宽
    cover_height = int(random.uniform(0.2, 0.5) * image_height)
    cover_width = int(cover_height * walker_ratio)
    # 缩放行人图片
    walker = walker.resize((cover_width, cover_height))
    # 随机行人图片位置
    x = random.randrange(-cover_width, image_width)
    y = random.randrange(-cover_height, image_height)

    # 覆盖
    image.paste(walker, (x, y), walker)

walker_list = [Image.open(walker) for walker in Path('walker').glob('*')]

# 遍历文件夹中的所有图片
for filename in os.listdir(image_folder):
    if filename.endswith(".jpg") or filename.endswith(".png"):
        # 读取原图像
        image_path = os.path.join(image_folder, filename)
        img = Image.open(image_path)

        # 随机覆盖行人
        for _ in range(random.randint(1, 4)):
            cover_walker(random.choice(walker_list), img)

        # 保存修改后的图像
        img.save(os.path.join('new_save', filename))
